package com.gwc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(urlPatterns = {"/httpServletRequest"})
public class HttpServletRequest extends HttpServlet {
    @Override
    protected void service(javax.servlet.http.HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求行
        System.out.println(req.getMethod());
        System.out.println(req.getScheme()); // 请求协议
        System.out.println(req.getProtocol()); // 请求协议及版本号
        System.out.println(req.getRequestURI()); // 项目内路径 /demo02/httpServletRequest
        System.out.println(req.getRequestURL()); // 项目内完整的url http://ip:port/demo02/a.html
        System.out.println(req.getLocalPort()); // 本应用容器的端口号
        System.out.println(req.getServerPort()); // 客户端发起请求时的目的端口号，可能会使用到代理
        System.out.println(req.getRemotePort()); // 客户端软件的端口号
        // 请求头
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String head = headerNames.nextElement();
            System.out.println(head+": "+req.getHeader(head));
        }
    }
}
